package com.itheima.health.controller;

import com.itheima.health.common.PageParam;
import com.itheima.health.common.R;
import com.itheima.health.common.ResultPageData;
import com.itheima.health.model.pojos.Setmeal;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.AliOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private AliOssUtil aliOssUtil;

    @PostMapping("/findPage")
    public ResultPageData findPage(@RequestBody PageParam pageParam){
        ResultPageData resultPageData = setmealService.findPage(pageParam);
        return resultPageData;
    }
    @PostMapping("/upload")
    public R upload( MultipartFile imgFile) throws IOException {
        String originalFilename = imgFile.getOriginalFilename();

        String fileType = originalFilename.substring(originalFilename.lastIndexOf("."));

        String url = aliOssUtil.upload(imgFile.getBytes(), UUID.randomUUID().toString()+fileType);
        return R.success(url);
    }
    @PostMapping("/add")
    public R insert(@RequestBody Setmeal setmeal, @RequestParam List<Integer> checkgroupIds){
        setmealService.insert(setmeal,checkgroupIds);
        return R.success();
    }

}
